﻿CREATE PROCEDURE [dbo].[SaveJob]
(
	@JobID varchar(50), 
	@RunID varchar(50),
	@ComponentID varchar(50),
	@ComponentTypeName varchar(500),
	@Cancelled bit,
	@Dependency varchar(500),
	@HasError bit,
	@IsFirstJob bit,
	@IsLastJob bit,
	@JobInstructionTypeName varchar(500),
	@JobOutputTypeName varchar(500),
	@JobInstructionID int,
	@JobOutputID int,
	@Priority int,
	@ReceiveTime datetime,
	@SubmissionTime datetime,
	@StartTime datetime,
	@FinishTime datetime
) as 
begin 
	declare @existCount int
	set @existCount=(select count (*) from Jobs where JobID=@JobID)
	
	if(@existCount>0)
	begin
		update Jobs 
		set 
			RunID=@RunID,
			ComponentID=@ComponentID,
			ComponentTypeName=@ComponentTypeName,
			Cancelled=@Cancelled,
			Dependency=@Dependency,
			HasError=@HasError,
			IsFirstJob=@IsFirstJob,
			IsLastJob=@IsLastJob,
			JobInstructionTypeName=@JobInstructionTypeName,
			JobOutputTypeName=@JobOutputTypeName,
			JobInstructionID=@JobInstructionID,
			JobOutputID=@JobOutputID,
			Priority=@Priority,
			ReceiveTime=@ReceiveTime,
			SubmissionTime=@SubmissionTime,
			StartTime=@StartTime,
			FinishTime=@FinishTime 
		where 
			JobID=@JobID
	end
	else
	begin
		insert into Jobs 
			(JobID,
			RunID,
			ComponentID,
			ComponentTypeName,
			Cancelled,
			Dependency,
			HasError,
			IsFirstJob,
			IsLastJob,
			JobInstructionTypeName,
			JobOutputTypeName,
			JobInstructionID,
			JobOutputID,
			Priority,
			ReceiveTime,
			SubmissionTime,
			StartTime,
			FinishTime) 
		values 
			(@JobID, 
			@RunID,
			@ComponentID,
			@ComponentTypeName,
			@Cancelled,
			@Dependency,
			@HasError,
			@IsFirstJob,
			@IsLastJob,
			@JobInstructionTypeName,
			@JobOutputTypeName,
			@JobInstructionID,
			@JobOutputID,
			@Priority,
			@ReceiveTime,
			@SubmissionTime,
			@StartTime,
			@FinishTime)
	end
end


